﻿
class SessionWatcher {
    constructor() {
        //通知对象
        this.watchSesssionId = null;
        this.lastAccessTime = null;
        this.sessionTimeout = 20 * 60 * 1000;
        this.SessionTimeoutHandler = null;
    }

    /**
     * 启动Session监听
     */
    watch() {
        var self = this;
        self.lastAccessTime = new Date();
        self.watchSesssionId = setInterval(function () {
            var now = new Date();
            if (now - self.lastAccessTime > self.sessionTimeout) {
                if (self.SessionTimeoutHandler) {
                    self.SessionTimeoutHandler();
                }
            }
        }, 1000);
    }

    /**
     * 停止Session监听
     */
    stop() {
        if (this.watchSesssionId) {
            clearInterval(this.watchSesssionId);
        }
    }
}
